package com.meihua.baoleyi.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.BaseActivity;
import com.meihua.baoleyi.bean.MemberPayBean;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.common.MyApp;
import com.meihua.baoleyi.dialog.MemberPayDialog;
import com.meihua.baoleyi.net.NetHelper;
import com.meihua.baoleyi.utils.ToastUtil;

import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 作者：jzm 2017/11/1.
 * 会员升级页面
 */

public class MemberDetailActivity extends BaseActivity {

    private static final String ACTION_NAME = "PayDialog";
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.submit)
    TextView submit;
    @Bind(R.id.webView)
    WebView mWebView;

    private MemberPayDialog dialog;
    private int level;
    private String memberData = "";
    private boolean isNeting = false;
    private MemberBroadcastReceiver mBroadcastReceiver;

    public static Intent createIntent(Context mContext, int level) {
        Intent intent = new Intent(mContext, MemberDetailActivity.class);
        intent.putExtra("level", level);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member_detail);
        ButterKnife.bind(this);
        level = getIntent().getIntExtra("level", 0);
        showWebView();
        initListener();
        registerBroadcastReceiver();
    }

    /**
     * 加载H5页面
     */
    private void showWebView() {

        mWebView.setWebViewClient(new WebViewClient() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        WebSettings webSetting = mWebView.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setDefaultTextEncodingName("utf-8");
        //携带数据请求
        String token = MyApp.getInstance().getToken();
        String user_id = MyApp.getInstance().getUser_id();
        mWebView.loadUrl(APIServer.MEMBER+token+"/"+user_id+"/"+level);
    }

    private void initListener() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isNeting) { //正在请求时不可点击
                    return;
                }
                if (memberData.isEmpty()) {
                    getServerData();
                } else {
                    if (dialog == null) {
                        dialog = MemberPayDialog.getInstance(MemberDetailActivity.this, memberData);
                    }
                    dialog.show(getSupportFragmentManager(), "dialog");
                }
            }
        });
    }

    /**
     * 会员充值列表等级详情
     */
    public void getServerData() {
        isNeting = true;
        TreeMap<String, String> treeMap = new TreeMap<String, String>();
        treeMap.put("user_id", MyApp.getInstance().getUser_id());
        NetHelper.post(APIServer.MEMBER_LEVEL, treeMap, true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                memberData = s;
                MemberPayBean bean = NetHelper.getBeanForJson(MemberPayBean.class, s);
                if (bean.getCode().equals("200")) {
                    isNeting = false;
                    if (dialog == null) {
                        dialog = MemberPayDialog.getInstance(MemberDetailActivity.this, s);
                    }
                    /**
                     * DialogFragment.show()处理异常, 有更好的方案请联系我
                     * email: zm.jin@qq.com
                     */
                    try {
                        dialog.show(getSupportFragmentManager(), "dialog");
                    } catch (IllegalStateException e) {
                    }

                } else {
                    isNeting = false;
                    ToastUtil.show(MemberDetailActivity.this, bean.getMsg());
                }
            }
        });
    }

    /**
     * 定义一个广播
     */
    public class MemberBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    }

    ;

    /**
     * 注册广播
     */
    private void registerBroadcastReceiver() {
        mBroadcastReceiver = new MemberBroadcastReceiver();
        IntentFilter mFilter = new IntentFilter();
        mFilter.addAction(ACTION_NAME);
        this.registerReceiver(mBroadcastReceiver, mFilter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //解注册
        this.unregisterReceiver(mBroadcastReceiver);
    }

}
